var LogoConfig = Vue.component('logoConfig', function (resolve, reject) {
    axios.get('views/logoConfig/logoConfig.html?timestamp=' + timestamp).then(function (res) {
        resolve({
            template: res.data,
            data(){
                return {
                    needAdminRole:CommonUtils.checkRole('admin'),
                    needAliMgrRole:CommonUtils.checkRole("alipay"),
                    tableData: [],
                    disableDetailForm:false,
                    dialogFormVisible: false,
                    form: {},
                    pageInfo:{
                        currentPage:1,
                        pageSize:10,
                        srch_LIKE_host:'',
                        srch_LIKE_bizCode:'',
                        totalNum:10,
                    },
                    editForm:{},
                    showEditWin:false,
                    formLabelWidth: '150px',
                    fileData:{mark:"product"},
                    uploadHead:{token:sessionStorage.token},
                    imageUrl:'',
                    editImageUrl:'',
                    dialogImageUrl: '',

                    dialogVisible: false,
                    fileAction:ctx+'/file/upload',
                    options: [{
                        value: '01',
                        label: '登陆首页'
                    }, {
                        value: '02',
                        label: '主页LOGO'
                    }, {
                        value: '03',
                        label: '二维码背景'
                    }, {
                        value: '04',
                        label: '二维码商标'
                    }, {
                        value: '05',
                        label: '系统标题'
                    }],
                }
            },
            methods:{

                beforeUpload (file) {
                    const isJPG = file.type === 'image/jpeg';
                    const isPNG = file.type === 'image/png';
                    const isLt2M = file.size / 1024 / 1024 < 2;

                    if (!isJPG && !isPNG) {
                        this.$message.error('上传图片只能是 JPG 或 PNG格式!');
                    }
                    if (!isLt2M) {
                        this.$message.error('上传图片大小不能超过 2MB!');
                    }
                    let currentForm = {};
                    if(this.showEditWin){
                        currentForm = this.editForm;
                    }else{
                        currentForm = this.form;
                    }

                    let sizeFlag = true;

                    let typeFlag = true;
                    if(CommonUtils.checkNull(currentForm.type)){
                        this.$message.error('请先选择配置类型');
                        typeFlag = false;
                    }
                    let width = 0;
                    let height = 0;
                    let compareType = 'ge';
                    let validFileSize = true;
                    switch(currentForm.type)
                    {
                        case '01':
                            width = 580;
                            height = 131;
                            compareType='ge';
                            break;
                        case '02':
                            width = 400;
                            height = 80;
                            compareType='le';
                            break;
                        case '03':
                            width = 750;
                            height = 1334;
                            break;
                        case '04':
                            width = 80;
                            height = 80;
                            break;
                        case '05':
                            validFileSize = false;
                            break;
                        default:
                            validFileSize = false;
                    }

                    if(validFileSize){
                        sizeFlag = new Promise(function(resolve, reject) {

                            let _URL = window.URL || window.webkitURL;
                            let img = new Image();
                            img.onload = function() {
                                let valid = false;
                               
                                if(compareType == 'eq'){
                                    valid = img.width == width && img.height == height
                                }
                                if(compareType == 'ge'){
                                    valid = img.width >= width && img.height >= height
                                }
                                if(compareType == 'le'){
                                    valid = img.width <= width && img.height <= height
                                }
                                valid ? resolve() : reject();
                            }
                            img.src = _URL.createObjectURL(file);
                        }).then(() => {
                            return file;
                        }, () => {

                            if (compareType == 'eq'){
                                this.$message.error('图片必须是'+width+'*'+height+'!');
                            }
                            if (compareType == 'ge'){
                                this.$message.error('图片必须大于等于'+width+'*'+height+'!');
                            }
                            if(compareType == 'le'){
                                this.$message.error('图片必须小于等于'+width+'*'+height+'!');
                            }
                            return Promise.reject();
                        });
                    }


                    return isLt2M && (isJPG || isPNG) && typeFlag && sizeFlag;
                },

                handleImgSuccess(res, file) {
                    if(res.status == '00'){
                        this.imageUrl = URL.createObjectURL(file.raw);
                        this.form.fileId = res.data.id;
                    }else{
                        this.$alert(res.msg, '', {
                            confirmButtonText: '确定'
                        });
                    }
                },

                handleEditImgSuccess(res, file) {
                    if(res.status == '00'){
                        this.editImageUrl = URL.createObjectURL(file.raw);
                        this.editForm.fileId = res.data.id;
                    }else{
                        this.$alert(res.msg, '', {
                            confirmButtonText: '确定'
                        });
                    }
                },

                handleCurrentChange: function(currentPage){
                    this.pageInfo.currentPage = currentPage;
                    this.initData();
                },
                initData:function(){
                    var url = APIs.logoConfig.query;
                    httpInvoker.get(url,{
                        params:this.pageInfo,
                        paramsSerializer: params => {
                            return Qs.stringify(params, { indices: false })
                        }
                    }).then(res=>{
                        if (res.data.status == '00') {
                            this.tableData = res.data.data.content;
                            this.pageInfo.totalPage = res.data.data.totalElements; 
                        }else {
                            this.$alert(res.data.msg, '', {
                                confirmButtonText: '确定'
                            });
                        }
                        
                    }).catch(()=>{

                    });
                },
                save:function(){
                    var url = APIs.logoConfig.save;
                    httpInvoker.post(url,this.form).then(res=>{
                        if ( res.data.status == '00') {
                            this.$alert(res.data.data, '', {
                                confirmButtonText: '确定'
                            });
                            this.initData();
                        }else {
                            this.$alert(res.data.msg, '', {
                                confirmButtonText: '确定'
                            });
                        }

                    }).catch(()=>{

                    });
                },
                onSubmit:function(){
                    this.$set(this.pageInfo,'currentPage',1);
                    this.initData();
                },
                edit:function(){
                    var url = APIs.logoConfig.update;
                    httpInvoker.post(url,this.editForm).then(res=>{
                        if ( res.data.status == '00') {
                            this.showEditWin=false;
                            this.$alert(res.data.data, '', {
                                confirmButtonText: '确定'
                            });
                            this.initData();
                        }else {
                            this.$alert(res.data.msg, '', {
                                confirmButtonText: '确定'
                            });
                        }

                    }).catch(()=>{

                    });
                },

                formatConfigType: function (row, column) {
                    return row.type == '01' ? '登陆首页' : row.type == '02' ? '主页LOGO': row.type == '03' ? '二维码背景'
                        : row.type == '04' ? '二维码商标': row.type == '05' ? '系统标题': '未知'
                },
                formatEnable: function (row, column) {
                    return row.isEnable == true ? '是' : row.isEnable == false ? '否': '未知'
                },
                editConfig(row){
                    this.disableDetailForm=false;
                    this.editForm=row;
                    if(CommonUtils.checkNull(row.imgFile)){
                        row.imgFile={};
                        row.imgFile.absPath = "";
                        row.imgFile.id = "";
                    }
                    this.editForm.id=row.id;
                    this.editForm.fileId=row.imgFile.id;
                    this.editImageUrl = row.imgFile.absPath;

                    this.editForm.imgFile = null;

                    this.showEditWin = true;
                },

                checkConfig(row){
                    this.editForm=row;
                    if(CommonUtils.checkNull(row.imgFile)){
                        row.imgFile={};
                        row.imgFile.absPath = "";
                        row.imgFile.id = "";
                    }
                    this.editImageUrl = row.imgFile.absPath;
                    this.disableDetailForm=true;
                    this.showEditWin = true;
                },
                deleteData(row){
                    this.$confirm('确定要删除吗？', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        httpInvoker.post(APIs.logoConfig.delete,{id:row.id}).then(res =>{
                            if(res.status == 200 && res.data.status == '00'){
                                this.$alert('删除成功', '', {
                                    confirmButtonText: '确定',
                                    callback: action => {
                                        this.initData();
                                    }

                                });
                            }else{
                                this.$alert(res.data.msg, '', {
                                    confirmButtonText: '确定'
                                });
                            }
                        })
                    }).catch(() => {

                    });
                }
               
            },
            mounted: function () {
                this.initData();
            },
        })
    })
})